#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

int main() {
	int n, x = 0, y, next_x, next_y;

	printf("This program creates a magic square of a specified size.\n"
		"The size must be an odd number between 1 and 99.\n"
		"Enter size of magic square : ");
	scanf("%d", &n);
	
	y = n / 2;

	int matrix[n][n];
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			matrix[i][j] = 0;
		}
	}

	matrix[x][y] = 1;
	for (int i = 2; i <= n*n; i++)
	{
		next_x = (x - 1 + n) % n;
		next_y = (y + 1 + n) % n;

		if (matrix[next_x][next_y]) {
			x = x + 1;
		}
		else {
			x = next_x;
			y = next_y;
		}
		matrix[x][y] = i;
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			printf("%4d", matrix[i][j]);
		}
		printf("\n");
	}

	return 0;
}
